package com.um.jdy.platform.activity.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.activity.entity.dto.CouponsDTO;
import com.um.jdy.common.activity.entity.po.Coupons;
import com.um.jdy.common.activity.entity.po.CouponsGoods;
import com.um.jdy.common.activity.entity.po.CouponsStation;
import com.um.jdy.common.activity.entity.vo.CouponsVO;
import com.um.jdy.common.activity.service.CouponsGoodsService;
import com.um.jdy.common.activity.service.CouponsService;
import com.um.jdy.common.activity.service.CouponsStationService;
import com.um.jdy.platform.activity.entity.dto.CouponsListDTO;
import com.um.jdy.platform.activity.entity.dto.CouponsPageDTO;
import com.um.jdy.platform.activity.entity.dto.SaveCouponsDTO;
import com.um.jdy.platform.activity.entity.enums.ActivityEnum;
import com.um.jdy.platform.activity.entity.vo.CouponsItemVO;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.manager.BasePlusManager;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.springboot.starter.orm.params.PageDTO;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 管理类：优惠券表【备份】
 */
@Component
public class CouponsManager extends BasePlusManager<CouponsService, Coupons, CouponsDTO, CouponsVO, CouponsPageDTO, CouponsListDTO> {

    @Autowired
    CouponsGoodsService couponsGoodsService;

    @Autowired
    CouponsStationService couponsStationService;


    public Page<CouponsItemVO> getPage(PageParams pageParams){
        Page<CouponsItemVO> page = new Page<>();
        page.setPageNo(pageParams.getPageNo());
        page.setPageSize(pageParams.getPageSize());

        pageParams.getCondition().put("c.com_id",getComId());

        page = service.getCMSViewModelPageBySqlName(page,"get_coupons_page","get_coupons_count",
                CouponsItemVO.class, pageParams.getProperties(),pageParams.getValues(),
                pageParams.getRangeEntitys(),pageParams.getOrderNames(),pageParams.getSortOns());
        return page;
    }


    public void addCoupons(SaveCouponsDTO saveCouponsDTO) {
        Coupons coupons = BeanUtil.toBean(saveCouponsDTO.getCoupons(), Coupons.class);
        coupons.setCreateTime(new Date());
        service.save(coupons);

        if(ActivityEnum.UseRange.Part.name().equals(coupons.getAvailable())) {

            if(StringUtil.equals(ActivityEnum.SceneType.Goods.name(),coupons.getSceneType())) {
                saveCouponsDTO.getGoodsIds().forEach(goodsId -> {
                    CouponsGoods couponsGoods = new CouponsGoods();
                    couponsGoods.setActivityCouponsId(coupons.getActivityCouponsId());
                    couponsGoods.setShopGoodsId(goodsId);
                    couponsGoodsService.save(couponsGoods);
                });
            }else{
                saveCouponsDTO.getStationIds().forEach(stationId -> {
                    CouponsStation couponsStation = new CouponsStation();
                    couponsStation.setActivityCouponsId(coupons.getActivityCouponsId());
                    couponsStation.setDeviceStationId(stationId);
                    couponsStationService.save(couponsStation);
                });
            }
        }
    }

    public void editCoupons(String activityCouponsId, SaveCouponsDTO saveCouponsDTO) {
        Coupons coupons = BeanUtil.toBean(saveCouponsDTO.getCoupons(), Coupons.class);
        coupons.setActivityCouponsId(activityCouponsId);
        service.save(coupons);

        couponsGoodsService.deleteTrue("activity_coupons_id", coupons.getActivityCouponsId());
        couponsStationService.deleteTrue("activity_coupons_id",coupons.getActivityCouponsId());

        if(ActivityEnum.UseRange.Part.name().equals(coupons.getAvailable())) {

            if(StringUtil.equals(ActivityEnum.SceneType.Goods.name(),coupons.getSceneType())) {
                saveCouponsDTO.getGoodsIds().forEach(goodsId -> {
                    CouponsGoods couponsGoods = new CouponsGoods();
                    couponsGoods.setActivityCouponsId(coupons.getActivityCouponsId());
                    couponsGoods.setShopGoodsId(goodsId);
                    couponsGoodsService.save(couponsGoods);
                });
            }else{
                saveCouponsDTO.getStationIds().forEach(stationId -> {
                    CouponsStation couponsStation = new CouponsStation();
                    couponsStation.setActivityCouponsId(coupons.getActivityCouponsId());
                    couponsStation.setDeviceStationId(stationId);
                    couponsStationService.save(couponsStation);
                });
            }
        }
    }

    public CouponsVO editStatus(String id, String status) {
        Coupons coupons = new Coupons();
        coupons.setActivityCouponsId(id);
        coupons.setStatus(status);
        service.update(coupons);
        return BeanUtil.toBean(coupons, CouponsVO.class);
    }

    public CouponsVO editActivityMerge(String id, String is_activity_merge) {
        Coupons coupons = new Coupons();
        coupons.setActivityCouponsId(id);
        coupons.setIsActivityMerge(is_activity_merge);
        service.update(coupons);
        return BeanUtil.toBean(coupons, CouponsVO.class);
    }

    @Transactional
    public Response delete(String id) {
        boolean result = service.delete(id);
        couponsGoodsService.delete("activity_coupons_id",id);
        couponsStationService.delete("activity_coupons_id",id);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response deleteIds(List<String> ids) {
        for(String id : ids){
            couponsGoodsService.delete("activity_coupons_id",id);
            couponsStationService.delete("activity_coupons_id",id);
        }
        boolean result = service.deleteIds(ids);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response<List<CouponsVO>> getList(String sceneType) {
        List<Coupons> list = service.getList("scene_type",sceneType,"create_time","desc");
        return Response.success(UMBeanUtils.copyBeanList(list,CouponsVO.class));
    }
}
